<%@ include file="/include/taglib.jsp"%>
<c:choose>
	<c:when test="${item.class.name == 'org.computate.form.item.FormField'}">
		<c:choose>
			<c:when test="${empty user and item.required}">
				<c:set var="itemClass" value="required"/>
			</c:when>
			<c:otherwise>
				<c:set var="itemClass" value=""/>
			</c:otherwise>
		</c:choose>
		<c:choose>
			<c:when test="${item.c.name == 'java.lang.Boolean' or item.c.name == 'boolean'}">
				<c:choose>
					<c:when test="${fn:length(item.hiddenItems) gt 0 and item.parent.class.name == 'org.computate.form.item.FormGroup'}">
						<input name="${fn:escapeXml(item.name)}" type="radio" id="${fn:escapeXml(item.name)}_yes" value="true" onchange="$('.hiddenField_${fn:escapeXml(item.name)}').show();"/>
						<label for="${fn:escapeXml(item.name)}_yes">Yes</label>
						<input name="${fn:escapeXml(item.name)}" type="radio" id="${fn:escapeXml(item.name)}_no" value="false" onchange="$('.hiddenField_${fn:escapeXml(item.name)}').hide();"/>
						<label for="${fn:escapeXml(item.name)}_no">No</label>
						</fieldset>
						</div>
						</div>
						<% pageContext.setAttribute("item" + pageContext.getAttribute("itemLevel", PageContext.REQUEST_SCOPE), pageContext.getAttribute("item", PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE); %>
						<c:forEach var="itemField" items="${item.hiddenItems}" varStatus="status">
							<c:choose>
								<c:when test="${(status.index mod item.parent.columns) == 0}">
									<c:set var="blockClass" value="ui-block-a"/>
								</c:when>
								<c:when test="${(status.index mod item.parent.columns) == 1}">
									<c:set var="blockClass" value="ui-block-b"/>
								</c:when>
								<c:when test="${(status.index mod item.parent.columns) == 3}">
									<c:set var="blockClass" value="ui-block-c"/>
								</c:when>
								<c:when test="${(status.index mod item.parent.columns) == 4}">
									<c:set var="blockClass" value="ui-block-d"/>
								</c:when>
								<c:when test="${(status.index mod item.parent.columns) == 5}">
									<c:set var="blockClass" value="ui-block-e"/>
								</c:when>
							</c:choose>
							<div class="${blockClass} hiddenField_${fn:escapeXml(item.name)}" class="hide">
								<% pageContext.setAttribute("itemField" + pageContext.getAttribute("itemLevel", PageContext.REQUEST_SCOPE), pageContext.getAttribute("itemField", PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE); %>
								<c:set var="item" value="${itemField}" scope="request"/>
								<c:set var="itemLevel" value="${itemLevel + 1}" scope="request"/>
								<c:choose>
									<c:when test="${item.c.name == 'java.lang.Boolean' or item.c.name == 'boolean'}">
										<div data-role="fieldcontain">
											<fieldset data-role="controlgroup" data-type="horizontal">
												<legend>${fn:escapeXml(item.label)}</legend>
												<jsp:include page="/fragment/FormItem.jsp"/>
												<c:set var="itemLevel" value="${itemLevel - 1}" scope="request"/>
											</fieldset>
										</div>
									</c:when>
									<c:otherwise>
										<jsp:include page="/fragment/FormItem.jsp"/>
										<c:set var="itemLevel" value="${itemLevel - 1}" scope="request"/>
									</c:otherwise>
								</c:choose>
								<% pageContext.setAttribute("itemField", pageContext.getAttribute("itemField" + pageContext.getAttribute("itemLevel", PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE); %>
								<% pageContext.setAttribute("item", pageContext.getAttribute("item" + pageContext.getAttribute("itemLevel", PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE); %>
							</div>
							<c:if test="${status.last}">
								<div>
								<div>
								<fieldset>
								<legend></legend>
							</c:if>
						</c:forEach>
					</c:when>
					<c:when test="fn:length(item.hiddenItems) gt 0">
						<legend>${fn:escapeXml(item.label)}</legend>
						<input name="${fn:escapeXml(item.name)}" type="radio" id="${fn:escapeXml(item.name)}_yes" value="true" onchange="$('#${fn:escapeXml(item.name)}_collapsible').trigger('expand');"/>
						<label for="${fn:escapeXml(item.name)}_yes">Yes</label>
						<input name="${fn:escapeXml(item.name)}" type="radio" id="${fn:escapeXml(item.name)}_no" value="false" onchange="$('#${fn:escapeXml(item.name)}_collapsible').trigger('collapse');"/>
						<label for="${fn:escapeXml(item.name)}_no">No</label>
						<div id="${fn:escapeXml(item.name)}_collapsible" data-role="collapsible">
							<% pageContext.setAttribute("item" + pageContext.getAttribute("itemLevel", PageContext.REQUEST_SCOPE), pageContext.getAttribute("item", PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE); %>
							<c:forEach var="itemField" items="${item.hiddenItems}">
								<% pageContext.setAttribute("itemField" + pageContext.getAttribute("itemLevel", PageContext.REQUEST_SCOPE), pageContext.getAttribute("itemField", PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE); %>
								<c:set var="item" value="${itemField}" scope="request"/>
								<c:set var="itemLevel" value="${itemLevel + 1}" scope="request"/>
								<jsp:include page="/fragment/FormItem.jsp"/>
								<c:set var="itemLevel" value="${itemLevel - 1}" scope="request"/>
								<% pageContext.setAttribute("itemField", pageContext.getAttribute("itemField" + pageContext.getAttribute("itemLevel", PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE); %>
								<% pageContext.setAttribute("item", pageContext.getAttribute("item" + pageContext.getAttribute("itemLevel", PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE); %>
							</c:forEach>
						</div>
					</c:when>
					<c:otherwise>
						<input name="${fn:escapeXml(item.name)}" type="radio" id="${fn:escapeXml(item.name)}_yes" value="true" onchange="$('#${fn:escapeXml(item.name)}_collapsible').trigger('expand');"/>
						<label for="${fn:escapeXml(item.name)}_yes">Yes</label>
						<input name="${fn:escapeXml(item.name)}" type="radio" id="${fn:escapeXml(item.name)}_no" value="false" onchange="$('#${fn:escapeXml(item.name)}_collapsible').trigger('collapse');"/>
						<label for="${fn:escapeXml(item.name)}_no">No</label>
					</c:otherwise>
				</c:choose>
			</c:when>
			<c:when test="${item.c.name == 'java.util.Date'}">
				<input name="${fn:escapeXml(item.name)}" type="text" class="input_dob ${itemClass}" id="${fn:escapeXml(item.name)}" placeholder="${fn:escapeXml(item.label)}" title="${fn:escapeXml(item.label)}"/>
			</c:when>
			<c:when test="${not empty item.stars and (item.c.name == 'java.lang.Integer' or item.c.name == 'int')}">
				<c:forEach var="i" begin="1" end="${item.stars}" step="1" varStatus ="status">
					<input name="${fn:escapeXml(item.name)}" type="radio" class="star" value="${i}"/>
				</c:forEach>
			</c:when>
			<c:when test="${item.c.name == 'java.util.List' and not empty item.itemName}">
				<c:if test="${not (empty item.parent)}"><label for="${fn:escapeXml(item.name)}">${fn:escapeXml(item.label)}</label></c:if>
				<c:forEach items="${item.items}" var="itemField">
					<input id="${fn:escapeXml(item.name)}-${itemField.id}" name="${fn:escapeXml(item.name)}" type="checkbox" value="${fn:escapeXml(itemField.id)}" />
					<label for="${fn:escapeXml(item.name)}-${itemField.id}">${fn:escapeXml(itemField.name)}</label>
				</c:forEach>
				
				<c:if test="${not empty user}">
					<legend></legend>
			
					<div>
						<a href="/page/item/${fn:escapeXml(item.itemName)}/${fn:escapeXml(item.itemName)}Index.jsp">Edit ${fn:escapeXml(item.itemPluralName)}</a>
					</div>
					<input data-inline="true" type="text" name="${fn:escapeXml(item.itemName)}_name" id="${fn:escapeXml(item.itemName)}_name" placeholder="${fn:escapeXml(item.itemDisplayName)} Name" />
					<button data-inline="true" type="submit" data-icon="plus" name="submit" value="${fn:escapeXml(item.itemName)}">Add ${fn:escapeXml(item.itemDisplayName)}</button>
				</c:if>
			</c:when>
			<c:when test="${item.c.name == 'java.util.List' and not empty item.form}">
				<table class="autoTable" id="autoTable_${fn:escapeXml(item.name)}">
					<thead>
						<tr>
							<c:forEach var="formField" items="${item.form.items}" varStatus="status">
								<c:if test="${formField.class.name == 'org.computate.form.item.FormField'}">
									<th class="${fn:escapeXml(formField.name)}">
										<c:if test="${status.first}">
											<input type="hidden" name="${fn:escapeXml(item.name)}RowCount" class="${fn:escapeXml(item.name)}RowCount"/>
										</c:if>
										${fn:escapeXml(formField.label)}
									</th>
								</c:if>
							</c:forEach>
							<th></th>
						</tr>
					</thead>
					<tbody>
						<tr>
							<c:forEach var="formField" items="${item.form.items}" varStatus="status">
								<c:if test="${formField.class.name == 'org.computate.form.item.FormField'}">
									<td>
										<input type="text" name="${fn:escapeXml(item.name)}_${fn:escapeXml(formField.name)}"/>
									</td>
								</c:if>
							</c:forEach>
							<td>
								<input type="button" value="Delete" class="${fn:escapeXml(item.name)}Delete"/>
							</td>
						</tr>
					</tbody>
				</table>
			</c:when>
			<c:when test="${not empty item.itemName and item.dropdown}">
				<select name="${fn:escapeXml(item.name)}" id="${fn:escapeXml(item.name)}" data-native-menu="false" title="${fn:escapeXml(item.label)}">
					<option value="-1">${fn:escapeXml(item.label)}</option>
					<c:forEach items="${item.items}" var="itemField">
						<option value="${fn:escapeXml(itemField.id)}">${fn:escapeXml(itemField.name)}</option>
					</c:forEach>
				</select>
				
				<c:if test="${not empty user}">
					<div>
						<a href="/page/item/${fn:escapeXml(item.itemName)}/${fn:escapeXml(item.itemName)}Index.jsp">Edit ${fn:escapeXml(item.itemPluralName)}</a>
					</div>
					<input data-inline="true" type="text" name="${fn:escapeXml(item.itemName)}_name" id="${fn:escapeXml(item.itemName)}_name" placeholder="${fn:escapeXml(item.itemDisplayName)} Name" />
					<button data-inline="true" type="submit" data-icon="plus" name="submit" value="${fn:escapeXml(item.itemName)}">Add ${fn:escapeXml(item.itemDisplayName)}</button>
				</c:if>
			</c:when>
			<c:when test="${not empty item.itemName}">
				<input id="${fn:escapeXml(item.name)}-None" name="${fn:escapeXml(item.name)}" type="radio" value="${fn:escapeXml(item.noneLabel)}" checked="checked" />
				<label for="${fn:escapeXml(item.name)}-None">${fn:escapeXml(item.noneLabel)}</label>
				<c:forEach items="${item.items}" var="itemField">
					<input id="${fn:escapeXml(item.name)}-${itemField.id}" name="${fn:escapeXml(item.name)}" type="radio" value="${fn:escapeXml(itemField.id)}" />
					<label for="${fn:escapeXml(item.name)}-${itemField.id}">${fn:escapeXml(itemField.name)}</label>
				</c:forEach>
				
				<c:if test="${not empty user}">
					<div>
						<a href="/page/item/${fn:escapeXml(item.itemName)}/${fn:escapeXml(item.itemName)}Index.jsp">Edit ${fn:escapeXml(item.itemPluralName)}</a>
					</div>
					<input data-inline="true" type="text" name="${fn:escapeXml(item.itemName)}_name" id="${fn:escapeXml(item.itemName)}_name" placeholder="${fn:escapeXml(item.itemDisplayName)} Name" />
					<button data-inline="true" type="submit" data-icon="plus" name="submit" value="${fn:escapeXml(item.itemName)}">Add ${fn:escapeXml(item.itemDisplayName)}</button>
				</c:if>
			</c:when>
			<c:otherwise>
				<label for="${fn:escapeXml(item.name)}" class="ui-hidden-accessible">${fn:escapeXml(item.label)}</label>
				<c:choose>
					<c:when test="${fn:contains(item.name, 'phone') or fn:contains(item.name, 'Phone')}">
						<c:set var="itemClass" value="${itemClass} input_phone"/>
					</c:when>
					<c:otherwise>
						<c:set var="itemClass" value="${itemClass}"/>
					</c:otherwise>
				</c:choose>
				<c:choose>
					<c:when test="${item.textarea}">
						<textarea name="${fn:escapeXml(item.name)}" id="${fn:escapeXml(item.name)}" placeholder="${fn:escapeXml(item.label)}" title="${fn:escapeXml(item.label)}"></textarea>
					</c:when>
					<c:otherwise>
						<input name="${fn:escapeXml(item.name)}" type="text" class="${itemClass}" id="${fn:escapeXml(item.name)}" placeholder="${fn:escapeXml(item.label)}" title="${fn:escapeXml(item.label)}"/>
					</c:otherwise>
				</c:choose>
			</c:otherwise>
		</c:choose>
	</c:when>
	<c:when test="${item.class.name == 'org.computate.form.item.FormGroup'}">
		<div data-role="collapsible" data-collapsed="false">
			<h3>${fn:escapeXml(item.label)}</h3>
			<p>${fn:escapeXml(item.text)}</p>
			<div>
				<c:choose>
					<c:when test="${item.columns gt 1}">
						<c:choose>
							<c:when test="${item.columns == 2}">
								<c:set var="gridClass" value="ui-grid-a"/>
							</c:when>
							<c:when test="${item.columns == 3}">
								<c:set var="gridClass" value="ui-grid-b"/>
							</c:when>
							<c:when test="${item.columns == 4}">
								<c:set var="gridClass" value="ui-grid-c"/>
							</c:when>
							<c:when test="${item.columns == 5}">
								<c:set var="gridClass" value="ui-grid-d"/>
							</c:when>
						</c:choose>
						<div class="${gridClass}">
							<% pageContext.setAttribute("item" + pageContext.getAttribute("itemLevel", PageContext.REQUEST_SCOPE), pageContext.getAttribute("item", PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE); %>
							<c:forEach var="itemField" items="${item.children}" varStatus="status">
								<% pageContext.setAttribute("itemField" + pageContext.getAttribute("itemLevel", PageContext.REQUEST_SCOPE), pageContext.getAttribute("itemField", PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE); %>
								
								<c:choose>
									<c:when test="${(status.index mod item.columns) == 0}">
										<c:set var="blockClass" value="ui-block-a"/>
									</c:when>
									<c:when test="${(status.index mod item.columns) == 1}">
										<c:set var="blockClass" value="ui-block-b"/>
									</c:when>
									<c:when test="${(status.index mod item.columns) == 2}">
										<c:set var="blockClass" value="ui-block-c"/>
									</c:when>
									<c:when test="${(status.index mod item.columns) == 3}">
										<c:set var="blockClass" value="ui-block-d"/>
									</c:when>
									<c:when test="${(status.index mod item.columns) == 4}">
										<c:set var="blockClass" value="ui-block-e"/>
									</c:when>
								</c:choose>
								
								<div class="${blockClass}">
									<c:set var="item" value="${itemField}" scope="request"/>
									<c:set var="itemLevel" value="${itemLevel + 1}" scope="request"/>
									<c:choose>
										<c:when test="${item.c.name == 'java.lang.Boolean' or item.c.name == 'boolean'}">
											<div data-role="fieldcontain">
												<fieldset data-role="controlgroup" data-type="horizontal">
													<legend>${fn:escapeXml(item.label)}</legend>
													<jsp:include page="/fragment/FormItem.jsp"/>
													<c:set var="itemLevel" value="${itemLevel - 1}" scope="request"/>
												</fieldset>
											</div>
										</c:when>
										<c:otherwise>
											<jsp:include page="/fragment/FormItem.jsp"/>
											<c:set var="itemLevel" value="${itemLevel - 1}" scope="request"/>
										</c:otherwise>
									</c:choose>
									<% pageContext.setAttribute("itemField", pageContext.getAttribute("itemField" + pageContext.getAttribute("itemLevel", PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE); %>
									<% pageContext.setAttribute("item", pageContext.getAttribute("item" + pageContext.getAttribute("itemLevel", PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE); %>
								</div>
							</c:forEach>
							
						</div>
					</c:when>
					<c:otherwise>
						<% pageContext.setAttribute("item" + pageContext.getAttribute("itemLevel", PageContext.REQUEST_SCOPE), pageContext.getAttribute("item", PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE); %>
						<c:forEach var="itemField" items="${item.children}">
							<% pageContext.setAttribute("itemField" + pageContext.getAttribute("itemLevel", PageContext.REQUEST_SCOPE), pageContext.getAttribute("itemField", PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE); %>
							<c:set var="item" value="${itemField}" scope="request"/>
							<c:set var="itemLevel" value="${itemLevel + 1}" scope="request"/>
							<jsp:include page="/fragment/FormItem.jsp"/>
							<c:set var="itemLevel" value="${itemLevel - 1}" scope="request"/>
							<% pageContext.setAttribute("itemField", pageContext.getAttribute("itemField" + pageContext.getAttribute("itemLevel", PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE); %>
							<% pageContext.setAttribute("item", pageContext.getAttribute("item" + pageContext.getAttribute("itemLevel", PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE); %>
						</c:forEach>
						
					</c:otherwise>
				</c:choose>
			</div>
		</div>
	</c:when>
	<c:when test="${item.class.name == 'org.computate.form.item.FormParagraph'}">
		<p>${fn:escapeXml(item.text)}</p>
	</c:when>
</c:choose>
